<?php

/**
 * Other actions.
 *
 * @package    iEasy
 * @subpackage Other
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class otherActions extends sfActions
{
    public function getDataRows($sqlStr)
    {
        $dbBase = new DbBase();
        return $dbBase->getDataQuery($sqlStr);
    }
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
      //$this->forward('default', 'module');

  }
  public function executeJQuery(sfWebRequest $request)
  {
      $flag = $request->getParameter("submitflag");
      if($flag != null)
      {
          switch($flag)
          {
              case "question":
				  $content_id = $request->getParameter("id");
				  $question = $request->getParameter("question");
				  
				  $sql = "INSERT INTO [Questions] ([content_type],[content_id],[content]) ";
				  $sql.= "VALUES (2, $content_id, '$question')";

				  $this->getDataRows($sql);

                  break;
              case "survey":
                  if($request->getParameter("surveyFlag") != null)
                  {
                      if($this->getUser()->getAttribute("submitSurvey") == null)
                      {
                         $this->getUser()->setAttribute("submitSurvey",1);
                         return $this->renderComponent("other","updateSurvey",array("flag" =>$request->getParameter("surveyFlag"),"id"=>$request->getParameter("id")));
                      }
                      else
                          return $this->renderText("您已经参与该调查！请不要多次投票。");
                  }
                  else
                      return $this->renderComponent("other","updateSurvey",array("id"=>$request->getParameter("id")));
                  break;
              case "starpk":
				  $celebrity_pk_id = $request->getParameter("id");
				  $starpkFlag = $request->getParameter("starpkFlag");
				  $sql = "UPDATE celebrityPk set count".$starpkFlag."_i = count".$starpkFlag."_i+1 where celebrity_pk_id=".$celebrity_pk_id;
				  
				  $this->getDataRows($sql);

                  break;
              case "logout":
                 $this->logout();
                 $url = $request->getParameter("url");
                 if($url != null && (strrpos($url,"/user",0) >=0 || strrpos($url,"detail",0) >=0))
                     return $this->renderText("returnUrl");
                 else
                     return $this->renderText("ok");
                 break;
              case "login":
                  if($request->getParameter("userName") != null)
                  {
                      $message = $this->login($request->getParameter("userName"),$request->getParameter("pwd"));
                      return $this->renderText($message); //
                  }
                  else
                      return $this->renderText("no");
                  break;
			  case "vote":
                  if($request->getParameter("id") != null)
                  {
					$id = $request->getParameter("id");
					$sql = "update vote set count_i=(case when count_i is null then 0 else count_i+1 end) where vote_id =$id";
					//echo $sql;
					//exit;
					$this->getDataRows($sql);
                  }
                  return $this->renderText("no");
                  break;
			  case "videoinfo":
				  $id = $request->getParameter("id");
                  if($id != null)
                  {
					$sql = "update [VIdeo] set hit_i = (case hit_i when NULL then 1 else hit_i+1 end) where status_id !=1006 and video_id = ".$id;
					$this->getDataRows($sql);

					$sql = "select top 1 *, convert(varchar(10),publish_time,120) as currentTime, hit_i+hit_mask_i as hit from [VIdeo] where status_id !=1006 and video_id = ".$id;
					$result = $this->getDataRows($sql);
					$t = $result[0];

					/*
					$("#_title" ).html(info[0]);
					$("#_source").html(info[1]);
					$("#_artist").html(info[2]);
					$("#_time"  ).html(info[3]);
					$("#_tag"   ).html(info[4]);
					$("#_hit"   ).html(info[5]);
					*/
					echo $t['title'].';';
					echo $t['source'].';';
					echo $t['artist'].';';
					echo $t['currentTime'].';';
					echo $t['tag'].';';
					echo $t['hit'];
					exit;

                  }
                  return $this->renderText("no");
                  break;
          }
      }
    }

  public function login($userName,$pwd)
  {
      $connection = Doctrine_Manager::connection();
      $query = "SELECT user_id id, name  FROM users where name='".$userName."' and password='".$pwd."'";
      $statement = $connection->execute($query);
      $statement->execute();
      $user = $statement->fetch(PDO::FETCH_OBJ);

//      $users = Doctrine_Core::getTable("Users")
//            ->createQuery()
//              ->where("name= ?",$userName)
//              ->andWhere("password=?",$pwd)
//              ->execute();
//      $user = $users->getData();
      if($user != null)
      {
           $this->getUser()->setAttribute("userId",$user->id);
           $this->getUser()->setAttribute("userName",$userName);
           $user = Doctrine_Core::getTable("Users")->find($user->id);
           $user->setLast_login_time(date("Y-m-d H:i:s"));
           $user->save();
           return "ok";
      }
      return "no";
  }

  public function logout()
  {
      if($this->getUser()->getAttribute("userId") != null)
      {
           $user=Doctrine_Core::getTable("Users")->find($this->getUser()->getAttribute("userId"));
           $user->setLast_survey_time(date("Y-m-d H:i:s"));
           $user->save();
           $this->getUser()->setAttribute("userId",null);
           $this->getUser()->setAttribute("userName",null);
      }
      $this->getUser()->setAttribute("submitSurvey",null);
      $this->getUser()->setAttribute("submitStarpk",null);
  }

}
